'use strict';
var appModule =  angular.module('app');
appModule.controller('rmRiRegionController', [ '$scope', '$rootScope', '$state', '$timeout', 'toaster','$modal', '$window','$log',
	                                function($scope, $rootScope, $state, $timeout, toaster, $modal, $window, $log) {
	$rootScope.init=function(){  
		
        $scope.getTreeList();
    }  
    $scope.loading =false;
	$scope.param = {};
    $scope.getTreeList=function(){ 
        $scope.loading =true;
    	 $.ajax({
				type: 'POST',
	            dataType: 'json',
				contentType:'application/json;charset=UTF-8',
				url : '/dac/regionManage/region/treeList',
				data: angular.toJson($scope.param)
			}).then(function(result){
                $scope.loading =false;
                if(result.httpCode ==200){//成功
                    $scope.angularTreeList=[];  
                    //查询树  
                    $scope.orgTypeList=result.data;    
                    //创建根节点  
                    var root={};  
                    root.regionname="区域管理";  
                    root.level=0;  
                    root.sequence=1;  
                    root.parentid=0;  
                    root.regionid="0";  
                    root.children=result.data;  
                    $scope.angularTreeList.push(root);  
                    // $scope.treeOptions.data=$scope.angularTreeList;
                    
                    toaster.clear('*');
                    toaster.pop('success', '', "加载成功");
                }else{
                    toaster.clear('*');
                    toaster.pop('error', '', result.msg);
                    $scope.isDisabled = false;
                }
                $scope.$apply()
            });
    	
    }  

    $rootScope.init();

      

      
//     $scope.treeOptions = {
//         //拖拽操作 拖拽后会返回callback beforeDrop函数，我们可以重写这个函数，拖拽本质上是顺序的改变和层级的改变，所以是一个update操作
//         beforeDrop : function (e) {
//            var source = e.source.nodeScope.item;
//            var destRoot = e.dest.nodesScope.item ;
//            if(destRoot==undefined){
//                return $q.reject();
//            }
//            var destIndex=e.dest.index;
//            var dest=e.dest.nodesScope.item.children[destIndex];
//            if(dest==undefined){
//                return $q.reject();
//            }
//            if (source.parentTypeId!=dest.parentTypeId) {
//                return $q.reject();
//            }
//            var sourceSeq=source.sequence;
//            var destSeq=dest.sequence;
//            source.sequence=destSeq;
//            dest.sequence=sourceSeq;
// //           OrganizationService.updateOrgType(source).then(function(result) {
// //
// //           });
// //           OrganizationService.updateOrgType(dest).then(function(result) {
// //
// //           });
//            return;
//         }
//    };
    //新建节点
    $scope.newSubItem = function (item) {

        var modalInstance = $modal.open({
            templateUrl : 'addRegion.html',
            controller : 'addModalInstanceCtrl', // specify controller for modal
            size : '',
            resolve : {
                item : function() {
                    return item;
                }
            }
        });

    };

    //编辑节点
    $scope.editItem=function(item){
        var modalInstance = $modal.open({
            templateUrl : 'updateRegion.html',
            controller : 'edtModalInstanceCtrl', // specify controller for modal
            size : '',
            resolve : {
                item : function() {
                    return item;
                }
            }
        });
    }

    //删除节点
    $scope.delItem=function(item){
        var modalInstance = $modal.open({
            templateUrl : 'delRegion.html',
            controller : 'delModalInstanceCtrl', // specify controller for modal
            size : '',
            resolve : {
                item : function() {
                    return item;
                }
            }
        });

    }




} ]);



app.controller('addModalInstanceCtrl', function($scope,$rootScope, $window, $modalInstance,item,toaster) {

    $scope.regionname = item.regionname;

    $scope.parentregionid = item.regionid;

    $scope.addParam = { };
    $scope.addParam.parentid = item.regionid;

    // ok click
    $scope.addRegion = function() {
        $scope.addParam.regionname = $scope.addregionname;

        $.ajax({
            type: 'POST',
            dataType: 'json',
            contentType:'application/json;charset=UTF-8',
            url : '/dac/regionManage/region/create',
            data: angular.toJson($scope.addParam)
        }).then(function(result){
            if(result.httpCode ==200){//成功
                toaster.clear('*');
                toaster.pop('success', '', "添加成功");
                 $window.location.reload();
            }else{
                toaster.clear('*');
                toaster.pop('error', '', result.msg);
            }
        });
        $modalInstance.close();
    };
    // cancel click
    $scope.cancel = function() {
        $modalInstance.dismiss('cancel');
    }
})

app.controller('delModalInstanceCtrl', function($scope, $window, $modalInstance, item,toaster) {
    $scope.regionname = item.regionname;
    $scope.regionid = item.regionid;

    $scope.delParam = { };

    $scope.delParam.regionid = item.regionid;

    // ok click
    $scope.delRegion = function() {
        $.ajax({
            type: 'POST',
            dataType: 'json',
            contentType:'application/json;charset=UTF-8',
            url : '/dac/regionManage/region/delete',
            data: angular.toJson($scope.delParam)
        }).then(function(result){
            if(result.httpCode ==200){//成功
                toaster.clear('*');
                toaster.pop('success', '', "删除成功");
                $window.location.reload();
            }else{
                toaster.clear('*');
                toaster.pop('error', '', result.msg);
            }
        });

        $modalInstance.close();
    };
    // cancel click
    $scope.cancel = function() {
        $modalInstance.dismiss('cancel');
    }
})

app.controller('edtModalInstanceCtrl', function($scope, $window, $modalInstance, item,toaster) {
    $scope.regionname = item.regionname;
    $scope.regionid = item.regionid;

    $scope.edtParam = { };

    $scope.edtParam.regionid = item.regionid;


    // ok click
    $scope.updateRegion = function() {
        $scope.edtParam.regionname = $scope.edtregionname;
//		console.log(angular.toJson($scope.edtParam));

        $.ajax({
            type: 'POST',
            dataType: 'json',
            contentType:'application/json;charset=UTF-8',
            url : '/dac/regionManage/region/update',
            data: angular.toJson($scope.edtParam)
        }).then(function(result){
            if(result.httpCode ==200){//成功

                toaster.clear('*');
                toaster.pop('success', '', "更新成功");
                $window.location.reload();
            }else{
                toaster.clear('*');
                toaster.pop('error', '', result.msg);
            }

        });

        $modalInstance.close();
    };
    // cancel click
    $scope.cancel = function() {
        $modalInstance.dismiss('cancel');
    }


});




